(function () {
    'use strict';

    angular
        .module('app.AddReferral')
        .controller('AddReferralController', AddReferralController);

    AddReferralController.$inject = ['logger', '$state', '$rootScope', '$stateParams','DiagnosisProvider','$mdDialog','DefaultListsProvider','PatientDemoData','$timeout'];
    /* @ngInject */
    function AddReferralController(logger, $state, $rootScope, $stateParams, DiagnosisProvider, $mdDialog, DefaultListsProvider, PatientDemoData, $timeout) {
        var vm = this;
        vm.title = 'AddReferral';
        vm.password = "";
        vm.ReferralID = 0;
        vm.showPageControls = false;
        vm.onPaginate = onPaginate;
        vm.SerchDiagnosis = SerchDiagnosis;
        vm.SaveDiagnosis = SaveDiagnosis;
        vm.SavePatientData = SavePatientData;
        vm.SaveData = SaveData;
        vm.ReadOnly = false;
        vm.FilterPractice = FilterPractice;
        vm.FilterPracticePPG = FilterPracticePPG;
        vm.OtherPhysician = false;
        activate();
        
        vm.SelectedCodes = [];

        function activate() {
            $rootScope.ShowCrumbs = false;
            var promise = DiagnosisProvider.GetPatientDiagnosis(vm.ReferralID).then(function (response) {
                if (response.data.ErrorMessage != 'No Data') {
                    vm.TotalDiagnosis = DiagnosisProvider.SetAllDiagnosisList(response.data.PatientDiagnosis);
                    vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(0, 10);
                    vm.SelectedCodes = response.data.ChipData;
                    //$mdDialog.hide();
                }
            });
            
            vm.Physicians = DefaultListsProvider.GetPhysiciansList();
            vm.PracticesList = DefaultListsProvider.GetPracticesList();
            vm.StateList = DefaultListsProvider.GetStateList();
            vm.ReferringPhysicians = DefaultListsProvider.GetReferringPhysiciansList();

            if($rootScope.IsController == false){
                vm.ReferringPhysician = $rootScope.CurrentPhysicianID;
            }
        }



        function FilterPractice(Phy) {
            return Phy.CorporatePhysician == 'false' && Phy.PracticeID == vm.ReqPractice;
        }

        function FilterPracticePPG(Phy) {
            return Phy.CorporatePhysician == 'true' && Phy.PracticeID == vm.ReqPractice;
        }

        function onPaginate(page, limit) {
            vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(page - 1, limit);
        }

        function SerchDiagnosis() {
            showWait();
            var promise1 = DiagnosisProvider.GetFilteredDiagnosisList(vm.ReferralID, vm.SearchText).then(function (response) {
                vm.TotalDiagnosis = DiagnosisProvider.SetAllDiagnosisList(response.data.PatientDiagnosis);
                vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(0, 10);
                $mdDialog.hide();
            });
        }

        function SaveData() {
            var promise = PatientDemoData.SaveReferralProcessStatus(vm.ReferralID, vm.ReferralAccepted, vm.ReferralNotes, vm.PatientContact, vm.PatientContactNotes, vm.PatientVisit, vm.PatientVisitNotes, vm.RefPhysician, vm.RefPractice)
            .then(function (response) {
                if (response.data.SaveStatus = 'Saved') {
                    vm.ReadOnly = true;
                    $mdDialog.show(
                          $mdDialog.alert()
                            .parent(angular.element(document.body))
                            .clickOutsideToClose(true)
                            .title('Save Status')
                            .textContent('Referral updates have been saved.')
                            .ariaLabel('Save Satus Dialog')
                            .ok('Close')
                        );
                }
            })
        }

        function SavePatientData() {

            var promise1 = PatientDemoData.SavePatientData(vm.PatientID, vm.FirstName, vm.LastName, vm.DOB.format("mm/dd/yy"), vm.AddrLine1, vm.AddrLine2, vm.PatientState, vm.zipcode, vm.phno, vm.phonetype, vm.Email, vm.InsuranceInfo, vm.PlanNumber, vm.InsuranceInfo1, vm.PlanNumber1, vm.Notes, vm.ReqPractice, vm.ReferringPhysician, vm.PreferredPhysician, $rootScope.CurrentLoginID, vm.RefReason, vm.StatRequest, vm.PrefPhone, vm.EstablishedPatient).then(function (response) {

                if (response.data.SaveStatus == 'Saved') {
                    vm.ReadOnly = true;
                    vm.ReferralID = response.data.ErrorMessage;
                    vm.ShowDiagnosis = true;
                    $mdDialog.show(
                          $mdDialog.alert()
                            .parent(angular.element(document.body))
                            .clickOutsideToClose(true)
                            .title('Save Status')
                            .textContent('Patient Demographics have been saved.')
                            .ariaLabel('Save Satus Dialog')
                            .ok('Close')
                        );
                }
                $mdDialog.hide();
            });

        }


        function SaveDiagnosis() {
            showWait();
            var promise2 = DiagnosisProvider.SavePatientDiagnosis(vm.ReferralID, vm.SelectedCodes, vm.DiagnosisNotes)
                .then(function (response) {
                    if (response.data.ErrorMessage == "Saved") {
                        $mdDialog.show(
                              $mdDialog.alert()
                                .parent(angular.element(document.body))
                                .clickOutsideToClose(true)
                                .title('Save Status')
                                .textContent('Patient Diagnosis have been saved.')
                                .ariaLabel('Save Satus Dialog')
                                .ok('Close')
                            ).then(function (response) {
                                $state.go('dashboard');
                            });
                    }
                    $mdDialog.hide();
                });
        }

        function showWait() {
            $mdDialog.show({
                controller: 'diagnosisController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        };

    }
})();


